package ai.think.cloud.chat.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;

public class KCVAES {
    public static void main(String[] args) throws Exception {
        // 16 字节 AES 密钥
        byte[] keyBytes = hex("100B751A083838DF43F8E913839E25B9");
        byte[] zero16   = new byte[16];            // 16 字节 00

        Cipher c = Cipher.getInstance("AES/ECB/NoPadding");
        c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, "AES"));
        byte[] cipher = c.doFinal(zero16);

        byte[] kcv = Arrays.copyOf(cipher, 3);     // 取前 3 字节
        System.out.println("AES KCV = " + hex(kcv));   // 例：77DC84
    }

    /* 与上面相同的辅助方法 */
    private static String hex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) sb.append(String.format("%02X", b));
        return sb.toString();
    }
    private static byte[] hex(String s) {
        int len = s.length();
        byte[] out = new byte[len / 2];
        for (int i = 0; i < len; i += 2)
            out[i/2] = (byte) Integer.parseInt(s.substring(i, i+2), 16);
        return out;
    }
}